﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SaleStat.COM.Constant;
using SaleStat.COM.LogHelper;
using SaleStat.COM.Exceptions;


/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage : System.Web.UI.Page
{
    #region 全局定义
    /// <summary>
    /// 画面ID
    /// </summary>
    protected string m_PageId = "";
    #endregion

    #region 构造方法
    public BasePage()
    {
        // 根据类名取得画面ID
        m_PageId = this.GetType().Name.Replace("_aspx", "");
    }
    #endregion

    #region 判断是否session过期
    /// <summary>
    /// 初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Page_PreLoad(object sender, EventArgs e)
    {
        switch (m_PageId)
        {
            case "login":
            case "editpwd":
            case "error":
                break;
            default:
                if (!Session.IsNewSession)
                {

                    if (Session[SessionKeyConstant.SESSION_USERID] == null)
                    {
                        string strMsg = string.Empty;
                        //Session已过期，请重新登陆!  
                        strMsg = "Session已过期，请重新登陆! ";
                        Server.Transfer("../Error.aspx?type=Session.Exception&message=" + strMsg, false);
                    }
                }
                else
                {
                    string strMsg = string.Empty;
                    //该用户未登陆，请先登陆！  
                    strMsg = "该用户未登陆，请先登陆！";
                    Server.Transfer("../Error.aspx?type=Session.Exception&message=" + strMsg, false);
                }
                break;
        }
        ClearCache();
        //画面输入文本框空格处理
        TextBoxTrim();

    }
    #endregion

    #region 清空缓存
    /// <summary>
    /// 清空缓存
    /// </summary>
    public void ClearCache()
    {
        try
        {
            Response.Expires = 0;
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            Response.AddHeader("pragma", "no-cache");
            Response.AddHeader("cache-control", "private");
            Response.CacheControl = "no-cache";
        }
        catch (System.Exception ex)
        {

        }
    }
    #endregion

    #region 输出消息日志
    /// <summary>
    /// 输出消息日志 
    /// </summary>
    /// <param name="content"></param>
    /// <param name="operation"></param>
    public void f_Info(CommonConstant.OPERATIONS operation, string strMessage)
    {

        LogHelper.f_Info(operation, m_PageId, strMessage);

    }
    #endregion

    #region 输出错误日志
    /// <summary>
    /// 输出错误日志 
    /// </summary>
    /// <param name="content"></param>
    /// <param name="operation"></param>
    public void f_Error(CommonConstant.OPERATIONS operation, Exception ex)
    {
        if (ex is BusinessException)
        {
            //取得业务异常
            BusinessException bsEx = (BusinessException)ex;

            LogHelper.f_Error(operation, m_PageId, bsEx.StrMsg);
        }
        else
        {
            LogHelper.f_Error(operation, m_PageId, ex.Message);
        }

    }
    #endregion

    #region 输出错误日志
    /// <summary>
    /// 输出错误日志 
    /// </summary>
    /// <param name="content"></param>
    /// <param name="operation"></param>
    public void f_Error(CommonConstant.OPERATIONS operation, string strMessage)
    {

        LogHelper.f_Error(operation, m_PageId, strMessage);

    }
    #endregion

    #region 处理异常
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ex"></param>
    public void ExceptionHandler(Exception ex)
    {
        //业务异常
        if (ex is BusinessException)
        {
            //取得业务异常
            BusinessException bsEx = (BusinessException)ex;
            string strMsg = bsEx.StrMsg;
            this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "businessException", "<script>popupBusinessException('" + strMsg + "');</script>");

            return;
        }

        // 系统异常
        //定义漂移页面URL变量
        string url = string.Empty;

        //设置漂移页面URL
        url = "~/Error.aspx?message=" + Server.UrlEncode(ex.Message) + "&type=" + ex.GetType().FullName;

        //漂移到URL指定页面
        Server.Transfer(url, false);

    }
    #endregion

    #region TextBox去空格处理
    /// <summary>
    /// 画面的TextBox去空格处理
    /// </summary>
    private void TextBoxTrim()
    {
        //若当前画面使用了MasterPage,则从MasterPage的ContentPlaceHolder取得个别页面
        if (this.Page.Master != null)
        {
            //从MasterPage取得当前画面
            ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)this.Page.Master.FindControl("mainContent");

            int count = contentPlaceHolder.Controls.Count;

            for (int i = 0; i < count; i++)
            {
                //取得当前控件
                Control currentCtrl = contentPlaceHolder.Controls[i];

                //若当前控件为Panel,做以下处理
                if (currentCtrl.GetType().Name == "Panel")
                {
                    //遍历Panel控件的TextBox
                    foreach (Control ctrl in currentCtrl.Controls)
                    {
                        if (ctrl.GetType().Name == "TextBox")
                        {
                            ((TextBox)ctrl).Text = ((TextBox)ctrl).Text.Trim();
                        }
                    }
                }
                else
                {
                    if (currentCtrl.GetType().Name == "TextBox")
                    {
                        ((TextBox)currentCtrl).Text = ((TextBox)currentCtrl).Text.Trim();
                    }
                }
            }
        }
    }
     #endregion

    #region 弹出信息框
    public void Alert(string strMessage)
    {
        try
        {

            string js = "<script>alert('" + strMessage + "');</script>";

            Page.ClientScript.RegisterStartupScript(Page.GetType(), "warning", js);

        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
    #endregion

    #region 异常时关闭弹出画面
    /// <summary>
    /// 异常时关闭弹出画面 
    /// </summary>
    /// <param name="content"></param>
    /// <param name="operation"></param>
    public void CloseWinWithException(Exception ex)
    {
        string strMsg = ex.Message.Replace("'", "\"").Replace("\r\n", "");
        string strJs = "<script language= 'javascript'>CloseWinWithException('" + strMsg + "','" + ex.GetType().FullName + "')</script>";

        Page.ClientScript.RegisterStartupScript(Page.GetType(), "startup", strJs);
    }
    #endregion
}
